/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;
import java.io.Serializable;
import entity.Examen;
import entity.Usuario;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import utils.HibernateUtil;
import java.util.List;

/**
 *
 * @author NAYELI
 */
public class ExamenDAO implements Serializable{
     public static void main(String... a) {
        
     }



   /*Metodo que se encarga guardar un Examen
    * param: examen, usuario
    * return: idUsuario
    */
   public static synchronized String guardaExamen(Examen examen,Usuario usuario){
        Session session=null;
        Transaction transaction=null;
        try{

            session=HibernateUtil.getSessionFactory().openSession();
            transaction=session.beginTransaction();
            Usuario usu=null;
            usu=(Usuario)session.load(Usuario.class,new String(usuario.getIdUsuario()));
            examen.setIdUsuarioEntity(usuario);
            session.saveOrUpdate(examen);
            transaction.commit();
            session.close();
            return usuario.getIdUsuario();
         }catch(org.hibernate.exception.GenericJDBCException excepts){
            transaction.rollback();
            if(session!=null&&session.isOpen()){
                session.close();
             }
            return "-1";
         }catch(Exception e){
            transaction.rollback();
            if(session!=null&&session.isOpen()){
                session.close();
             }
            return "-1";
          }finally{
            if(session!=null&&session.isOpen()) {
                session.close();

             }
          }
   }

   //Método que se encarga de eliminar un perfil de usuario por id del perfil.
   //return:1 si se elimino  correctamente
      public static synchronized int eliminaExamen(int idExamen){
        Session session = null;
        Transaction transaction = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            System.out.println("Iniciando transaction de borrado");

            Usuario usuario=getExamenPorID(idExamen);
             session.delete(usuario);
             transaction.commit();

            session.close();
        } catch (org.hibernate.exception.GenericJDBCException excepts) {
            transaction.rollback();

            if (session != null && session.isOpen()) {
                session.close();
            }

        } catch (Exception e) {
            transaction.rollback();

            if (session != null && session.isOpen()) {
                session.close();
            }

        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }

        }





/*public static synchronized void ConsultaPreguntasConIncisos(){

  Session session=HibernateUtil.getSessionFactory().openSession();
   try{
                          String hql="FROM Inciso i inner join i.pregunta as p";
                          org.hibernate.Query query=session.createQuery(hql);
                          List results=query.list();
                          System.out.println(results.isEmpty()+"****************** L I S T A ***************");
                          Iterator i=results.iterator();
                          while(i.hasNext())
                          {

                              System.out.println(i.next());
                          }

                          session.close();
              }catch(org.hibernate.exception.GenericJDBCException excepts){
      if(session!= null&& session.isOpen())
    session.close();
               }catch(Exception e){
   if(session!= null&& session.isOpen())
        session.close();
      }finally{
        if(session!= null && session.isOpen())
      session.close();

     }
    }*/
     


       return 1;


    }
     //Método que se encarga de consultar una lista de Usuarios.
        public static List<Examen> getAllExameness(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		List <Examen>lista=null;
		try{
			String query ="FROM Examen";
			lista=session.createQuery(query).list();
			session.close();
		}catch(org.hibernate.exception.GenericJDBCException excepts){
			if(session!= null&& session.isOpen())
				session.close();


     
    





		}catch(Exception e){
			if(session!= null&& session.isOpen())
				session.close();

		} finally{
			if(session!= null && session.isOpen())
				session.close();

		}
		return lista;
	}

         //Método para modificar un Perfil de usuario por ID
         public static synchronized void ModificarExamenByName(Examen examen){

		Session session = null;
		Transaction transaction =null;

		try{
			 session=HibernateUtil.getSessionFactory().openSession();
			 transaction=session.beginTransaction();
		        session.update(examen);
			 transaction.commit();
			 session.close();

		} catch (org.hibernate.exception.GenericJDBCException excepts) {
            transaction.rollback();

            if (session != null && session.isOpen()) {
                session.close();
            }

        } catch (Exception e) {
            transaction.rollback();

            if (session != null && session.isOpen()) {
                session.close();
            }

        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }

        }
    }


         public static synchronized Usuario getExamenPorID(int idexamen){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Usuario usuario=null;
		try{
			String query="FROM Examen where idExamen="+idexamen;
			usuario=(Usuario)session.createQuery(query).uniqueResult();
			session.close();
		}catch(org.hibernate.exception.GenericJDBCException excepts){
			if(session!= null&& session.isOpen())
				session.close();

		}catch(Exception e){
			if(session!= null&& session.isOpen())
				session.close();

		} finally{
			if(session!= null && session.isOpen())
				session.close();
		}
		return usuario;
  }

}

